\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{protocol}

\RequirePackage{etoolbox}
\RequirePackage{url}
\RequirePackage{environ}
\RequirePackage{makecell}
\RequirePackage{mfirstuc}
\RequirePackage{localize}

% Initialize boolean flags
\newbool{@createTitlePage}
\newbool{@showHeader}
\setbool{@showHeader}{true}
\newbool{@onesided}

\DeclareOption{titlepage}{\setbool{@createTitlePage}{true}}
\DeclareOption{noheader}{\setbool{@showHeader}{false}}
\DeclareOption{onesided}{\setbool{@onesided}{true}}
\DeclareOption{simpleHeader}{\AtBeginDocument{\simpleHeader}}
\DeclareOption{simpleheader}{\AtBeginDocument{\simpleHeader}}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}

\AtBeginDocument{%
	% Specify header content
	\lohead{\ifbool{@showHeader}{%
		\ifbool{@createTitlePage}{\@shortAuthor}{\ifnumgreater{\thepage}{1}{\@shortAuthor}{}}%
	}{}}%
	\lehead{\ifbool{@showHeader}{\@shortAuthor}{}}%
	\chead{\ifbool{@showHeader}{%
		\ifbool{@createTitlePage}{\@shortTitle}{\ifnumgreater{\thepage}{1}{\@shortTitle}{}}%
	}{}}%
	\rohead{\ifbool{@showHeader}{\@date}{}}%
	\rehead{\ifbool{@showHeader}{\@date}{}}%
}


\ProcessOptions\relax


\LoadClass{scrartcl}

\RequirePackage{scrlayer-scrpage}
\RequirePackage{amsmath}


% load packages with conditional options
\RequirePackage[%
	top=30mm,
	bottom=35mm,
	a4paper,
	left=2cm,
	right=2cm
]{geometry}%
\ifbool{@onesided}{%
	\geometry{%
		left=3cm,
	}
}{%
	\geometry{%
		asymmetric,
		bindingoffset=1cm
	}
}


%%%%%%%%%%%%%%%%%%% LOCALISATION %%%%%%%%%%%%%%%%%%%
\setDefaultLocalisation{module}{module}
\setDefaultLocalisation{release}{release}
\setDefaultLocalisation{experimentDate}{experiment date}
\setDefaultLocalisation{groupNumber}{group number}
\setDefaultLocalisation{groupName}{group name}
\setDefaultLocalisation{assistant}{assistant}
\setDefaultLocalisation{assistant_f}{assistant}
\setDefaultLocalisation{initialRelease}{initial release}
\setDefaultLocalisation{secondRelease}{second release}
\setDefaultLocalisation{protocolResponsible}{protocol responsible}
\setDefaultLocalisation{protocolResponsible_f}{protocol responsible}
\setDefaultLocalisation{abstract}{abstract}

% ngerman
\setLocalisation{module}{ngerman}{Modul}
\setLocalisation{release}{ngerman}{Abgabe}
\setLocalisation{experimentDate}{ngerman}{Versuchsdatum}
\setLocalisation{groupNumber}{ngerman}{Gruppennummer}
\setLocalisation{groupName}{ngerman}{Gruppenname}
\setLocalisation{assistant}{ngerman}{Assistent}
\setLocalisation{assistant_f}{ngerman}{Assistentin}
\setLocalisation{initialRelease}{ngerman}{Erstabgabe}
\setLocalisation{secondRelease}{ngerman}{Zweitabgabe}
\setLocalisation{protocolResponsible}{ngerman}{Protokoll-Schreiber}
\setLocalisation{protocolResponsible_f}{ngerman}{Protokoll-Schreiberin}
\setLocalisation{abstract}{ngerman}{Abstract}

% german
\setLocalisation{module}{german}{Modul}
\setLocalisation{release}{german}{Abgabe}
\setLocalisation{experimentDate}{german}{Versuchsdatum}
\setLocalisation{groupNumber}{german}{Gruppennummer}
\setLocalisation{groupName}{german}{Gruppenname}
\setLocalisation{assistant}{germanerman}{Assistent}
\setLocalisation{assistant_f}{german}{Assistentin}
\setLocalisation{initialRelease}{german}{Erstabgabe}
\setLocalisation{secondRelease}{german}{Zweitabgabe}
\setLocalisation{protocolResponsible}{german}{Protokoll-Schreiber}
\setLocalisation{protocolResponsible_f}{german}{Protokoll-Schreiberin}
\setLocalisation{abstract}{german}{Abstract}


% commands for setting meta information
\newcommand{\@shortTitle}{\@title}
\DeclareRobustCommand{\shortTitle}[1]{\renewcommand{\@shortTitle}{#1}}
\newcommand{\@shortAuthor}{\@author}
\DeclareRobustCommand{\shortAuthor}[1]{\renewcommand{\@shortAuthor}{#1}}
\newcommand{\@assistant}{}
\newtoggle{@femaleAssistant}
\DeclareRobustCommand{\assistant}[2][m]{%
	\renewcommand{\@assistant}{#2}%
	\ifstrequal{#1}{f}{\toggletrue{@femaleAssistant}}{}%
	\ifstrequal{#1}{female}{\toggletrue{@femaleAssistant}}{}%
	\ifstrequal{#1}{w}{\toggletrue{@femaleAssistant}}{}%
	\ifstrequal{#1}{weiblich}{\toggletrue{@femaleAssistant}}{}%
}
\newcommand{\@experimentDate}{}
\DeclareRobustCommand{\experimentDate}[1]{\renewcommand{\@experimentDate}{#1}}
\newcommand{\@groupNumber}{}
\DeclareRobustCommand{\groupNumber}[1]{\renewcommand{\@groupNumber}{#1}}
\newcommand{\@groupName}{}
\DeclareRobustCommand{\groupName}[1]{\renewcommand{\@groupName}{#1}}
\newcommand{\@contact}{}
\DeclareRobustCommand{\contact}[1]{\renewcommand{\@contact}{#1}}
\DeclareRobustCommand{\email}[1]{%
	\ifdefempty{\@contact}{%
		\apptocmd{\@contact}{\url{#1}}{}{}%
	}{%
		\apptocmd{\@contact}{, \url{#1}}{}{}%
	}%
}
\newcommand{\@initialRelease}{}
\DeclareRobustCommand{\initialRelease}[1]{\renewcommand{\@initialRelease}{#1}}
\newcommand{\@secondRelease}{}
\DeclareRobustCommand{\secondRelease}[1]{\renewcommand{\@secondRelease}{#1}}
\newcommand{\@responsible}{}
\newtoggle{@femaleResponsible}
\DeclareRobustCommand{\responsible}[2][m]{%
	\renewcommand{\@responsible}{#2}%
	\ifstrequal{#1}{f}{\toggletrue{@femaleResponsible}}{}%
	\ifstrequal{#1}{female}{\toggletrue{@femaleResponsible}}{}%
	\ifstrequal{#1}{w}{\toggletrue{@femaleResponsible}}{}%
	\ifstrequal{#1}{weiblich}{\toggletrue{@femaleResponsible}}{}%
}
\newcommand{\@module}{}
\DeclareRobustCommand{\module}[1]{\renewcommand{\@module}{#1}}

\newcommand{\@release}{}
\DeclareRobustCommand{\release}[1]{\renewcommand{\@release}{#1}}

% Environment for an abstract
\newcommand{\@abstract}{}
\NewEnviron{titleAbstract}{%
	\global\let\@abstract\BODY%
}

% environment for inclusion of logos
\newcommand{\@logo}{}
\NewEnviron{logo}{%
	\global\let\@logo\BODY%
}


% command for only showing the date in the header
\DeclareRobustCommand{\simpleHeader}{\lohead{} \lehead{} \chead{}}

\def\uppercaseLetterAfterDoubleProtect\protect\protect#1{\uppercase{#1}}
\newcommand{\expandAndCapitalize}[1]{%
	\edef\@tmp{\noexpand\uppercaseLetterAfterDoubleProtect#1}%
	\@tmp%
}



% customize \maketitle
\renewcommand{\maketitle}{%
	\ifbool{@createTitlePage}{%
		\thispagestyle{empty}%
		\begin{center}%
			\ifdefempty{\@module}{}{{\Large \textbf{\expandAndCapitalize{\getLocalisation{module}}:} \@module}}
			
			\ifdefempty{\@release}{}{\vspace{8mm}\@release. \getLocalisation{release}}
			
			\vspace*{\fill}%
			
			\ifdefempty{\@logo}{}{\@logo}
			
			\vspace*{\fill}
					
			{\Huge\textbf{\@title}}
			
			\vspace{.5cm}
			{\Large-\;\@author\;-\linebreak[2]}%		
			\ifdefempty{\@contact}{}{\\\@contact}
			
			\vspace{.3cm}
			
			{
				\large
				\begin{tabular}{l<{\,:}@{\hspace{2mm}}l}
					\ifdefempty{\@experimentDate}{}{\expandAndCapitalize{\getLocalisation{experimentDate}} & \@experimentDate\\}
					\ifdefempty{\@groupNumber}{}{\expandAndCapitalize{\getLocalisation{groupNumber}} & \@groupNumber\\}
					\ifdefempty{\@groupName}{}{\expandAndCapitalize{\getLocalisation{groupName}} & \@groupName\\}
					\ifdefempty{\@assistant}{}{\iftoggle{@femaleAssistant}{\expandAndCapitalize{\getLocalisation{assistant_f}}}{\expandAndCapitalize{\getLocalisation{assistant}}} & \@assistant\\}
					\ifdefempty{\@initialRelease}{}{\expandAndCapitalize{\getLocalisation{initialRelease}} & \@initialRelease\\}
					\ifdefempty{\@secondRelease}{}{\expandAndCapitalize{\getLocalisation{secondRelease}} & \@secondRelease\\}
					\ifdefempty{\@responsible}{}{\iftoggle{@femaleResponsible}{\expandAndCapitalize{\getLocalisation{protocolResponsible_f}}}{\expandAndCapitalize{\getLocalisation{protocolResponsible}}} & \@responsible\\}
				\end{tabular}
			}
		\end{center}%
		%
		\ifdefempty{\@abstract}{}{%
			\vspace{1cm}
			\begin{minipage}{\textwidth}
				\section*{\expandAndCapitalize{\getLocalisation{abstract}}}%
				\@abstract%
			\end{minipage}
		}
			
		\vspace*{\fill}
		\pagebreak
%		\setcounter{page}{1}
	}{%
	%	\thispagestyle{plain}%
		\let\@loheadCopy\lohead
		\begin{center}\Large{\textbf{\@title}} \\ \large{-\;\@author\;-\linebreak[2]} \end{center}%
	}%
}

\rofoot{}
\cofoot{}
\lefoot{}
\cefoot{}
\ifbool{@onesided}{
	\cfoot{\thepage}
}{
	\lefoot{\thepage}
	\rofoot{\thepage}
}